package com.codingvip.graph;

public class GraphTest {
    private char[] nodes;
    private int[][] arcs;
    private int nodeNum;
    private boolean[] visited;

    public GraphTest(int nodeNum) {
        this.nodeNum = nodeNum;
        nodes = new char[nodeNum];
        visited = new boolean[nodeNum];
        for (int i = 0; i < nodeNum; i++) {
            for (int j = 0; j < nodeNum; j++) {
                arcs[i][j] = 0;
            }
        }
    }

    public void addEdge(int i, int j) {
        if (i == j) {
            return;
        }
        arcs[i][j] = 1;
    }

    public void print(int i) {
        System.out.print(nodes[i]+"  ");
    }

    private void traverse(int i) {
        visited[i] = true;
        //打印
        print(i);
        for (int j = 0; j < nodeNum; j++) {
            if (arcs[i][j] == 1 && visited[j]==false) {
                traverse(j);
            }
        }
    }
}
